﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace ncp.Business
{
    public class SystemOptionBll
    {
        /// <summary>
        /// 得到系统选项表
        /// </summary>
        public static DataTable GetSystemOptionTable(string p_Where, string p_Orderby, string pDataBase)
        {
            string sqlStr = "select * from sa_option";
            if (!string.IsNullOrEmpty(p_Where))
                sqlStr += " where " + p_Where;
            if (!string.IsNullOrEmpty(p_Orderby))
                sqlStr += " order by " + p_Orderby;
            return CommonBll.GetdtList(sqlStr, pDataBase);
        }

        /// <summary>
        /// 查看数据库中根据里面的值来判断执行的SQL语句
        /// </summary>
        public static int UpdateSystemOption(Dictionary<string, object> dic, string pDataBase)
        {
            string sqlStr = "";
            sqlStr += "IF EXISTS (select 1 from sa_option  where Option_Name='smtp_server') " +
            "update sa_option set option_value='" + dic["smtp_server"].ToString() + "' where Option_Name='smtp_server'" +
            " ELSE " +
            "insert into sa_option(option_value, option_name) values ('" + dic["smtp_server"].ToString() + "','smtp_server');";

            sqlStr += "IF EXISTS (select 1 from sa_option  where Option_Name='smtp_username') " +
            "update sa_option set option_value='" + dic["smtp_username"].ToString() + "' where Option_Name='smtp_username'" +
            " ELSE " +
            "insert into sa_option(option_value, option_name) values ('" + dic["smtp_username"].ToString() + "','smtp_username');";

            sqlStr += "IF EXISTS (select 1 from sa_option  where Option_Name='smtp_pwd') " +
            "update sa_option set option_value='" + dic["smtp_pwd"].ToString() + "' where Option_Name='smtp_pwd'" +
            " ELSE " +
            "insert into sa_option(option_value, option_name) values ('" + dic["smtp_pwd"].ToString() + "','smtp_pwd');";

            return CommonBll.ExecuteTransaction(sqlStr, pDataBase);
        }

        //清空数据
        public static int EmptyData(string data_obj,string pDataBase)
        {
            string sql = "";
            if(data_obj.Contains("A"))
                sql += "delete from bi_GoodsClass where GC_PId<>0; delete from bi_GoodsInfo;";
            if (data_obj.Contains("B"))
                sql += "delete from ads;";
            if (data_obj.Contains("C"))
                sql += "delete from bi_NewsInfo; ";
            if (data_obj.Contains("D"))
                sql += "delete from bi_PictureInfo; delete from bi_ProductInfo;";
            if (data_obj.Contains("E"))
                sql += "delete from bi_ClientInfo; delete from bi_CommentInfo; delete from bi_MessageInfo;delete from bi_VipInfo; ";
            if (data_obj.Contains("F"))
                sql += "delete from bi_FLink;";
            if (data_obj.Contains("G"))
                sql += "delete from sa_LogHandle; delete from sa_LogLogin; ";
            if (data_obj.Contains("H"))
                sql += " delete from sa_user where User_Account<>'admin'; ";
            if (data_obj.Contains("I"))
                sql += " delete from bi_Search; ";
            return CommonBll.ExecuteTransaction(sql, pDataBase);
        }
    }
}
